package com.hua.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.mapper.CommentMapper;
import com.hua.pojo.Comment;
import com.hua.pojo.User;
import com.hua.service.CommentService;
import com.hua.service.UserService;
import com.hua.utils.HuaUtil;
import com.hua.vo.WriteCommentData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author ONE-X
 * @date 2020/11/10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private CommentService commentService;

	@Autowired
	private UserService userService;

	@Override
	public Integer findQuestionByAuthorCount(String uid) {
		return commentMapper.findQuestionByAuthorCount(uid);
	}

	@Override
	public void addComment(WriteCommentData commentData) {
		// 获取当前用户
		String uid = HuaUtil.returnAuthentication();
		User user = userService.getOne(new QueryWrapper<User>().eq("uid", uid));
		// 评论id
		String cid = HuaUtil.getUuid();
		Comment comment =
				new Comment(0
				,cid
				,Integer.parseInt(commentData.getTopic_category())
				,commentData.getTopic_id()
				,user.getUid(),
				user.getUsername(),
				user.getAvatar(),
				commentData.getContext(),
				new Date()
				,null,null);

		commentService.save(comment);
	}

	@Override
	public List<Comment> findCommentQuestion(String uid) {
		return commentMapper.findCommentQuestion(uid);
	}

	@Override
	public List<Comment> findCommentBlog(String uid) {
		return commentMapper.findCommentBlog(uid);
	}


}
